import { useState } from 'react';
import { Button, Space, Form, Row, Col, Input, Table, Image } from 'antd';
import { PAGINATION_CONFIG } from '@/constants';
import type { CarTypeApply, QueryCarTypeApplyListParams } from './services';
import { queryCarTypeApplyList } from './services'
import { useRequest } from 'ahooks';
import type { ColumnsType } from 'antd/es/table/interface';
import dayjs from 'dayjs';

const AppUserPage: React.FC = () => {
  const [searchForm] = Form.useForm<QueryCarTypeApplyListParams>()
  const [searchParams, setSearchParams] = useState<QueryCarTypeApplyListParams>({
    pageNum: 1, pageSize: 10,
  })
  const list = useRequest(() => queryCarTypeApplyList(searchParams), {
    refreshDeps: [searchParams],
  })

  const onFinish = (formValues: any) => {
    setSearchParams({
      ...searchParams,
      keyword: formValues.keyword,
      pageNum: 1,
    })
  }

  const columns: ColumnsType<CarTypeApply> = [
    {
      title: '用户头像',
      dataIndex: 'userImg',
      render: val => val ? <Image style={{width: 40, height: 40}} src={val} /> : '--',
    },
    {
      title: '用户昵称',
      dataIndex: 'nickname',
      render: val => val || '--'
    },
    {
      title: '用户手机号',
      dataIndex: 'telephone',
      render: val => val || '--'
    },
    {
      title: '车型名称',
      dataIndex: 'carType',
    },
    {
      title: '申请时间',
      dataIndex: 'createTime',
      render: createTime => dayjs(createTime).format('YYYY-MM-DD HH:mm:ss')
    },
  ]
  
  return (
    <div className='container'>
      <div className="card search-card">
        <div className="form-area">
          <Form name='searchCarTypeForm' form={searchForm} onFinish={onFinish}>
            <Row gutter={[20, 20]}>
              <Col span={6}>
                <Form.Item label='车型查询' name='keyword'>
                  <Input placeholder='请输入' />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Space>
                  <Button type='primary' htmlType='submit' loading={list.loading}>筛选</Button>
                  <Button htmlType='reset'>重置</Button>
                </Space>
              </Col>
            </Row>
          </Form>
        </div>
      </div>
      <div className="card content-card">
        <Table<CarTypeApply>
          rowKey='id'
          loading={list.loading}
          dataSource={list.data?.data}
          columns={columns}
          scroll={{y: 700, x: 'max-content'}}
          pagination={{
            ...PAGINATION_CONFIG,
            current: searchParams.pageNum,
            pageSize: searchParams.pageSize,
            total: list.data?.totalCount || 0,
            onChange: (page, pageSize) => {
              setSearchParams({
                ...searchParams,
                pageNum: page === searchParams.pageNum ? 1 : page,
                pageSize,
              })
            }
          }}
        />
      </div>
    </div>
  );
};

export default AppUserPage;
